package com.college.collegeinfomanage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.college.collegeinfomanage.dto.UniversityDTO;
import com.college.collegeinfomanage.entity.University;
import com.college.collegeinfomanage.service.UniversityService;
import com.college.collegeinfomanage.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/university")
public class UniversityController {

    @Resource
    private UniversityService universityService;

    /**
     * 获取全部大学信息列表
     * @return
     */
    @GetMapping("/list")
    public Result page(UniversityDTO universityDTO){
        log.info("请求参数" + universityDTO);
        IPage<University> page = universityService.getPage(universityDTO);
        return Result.success(page);
    }

    /**
     * 获取大学信息列表
     */
    @GetMapping("/all/list")
    public Result allList(){
        log.info("请求参数");
        return Result.success(universityService.list());
    }

    /**
     * 添加大学信息
     * @param university
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody University university){
        log.info("请求参数" + university);
        boolean save = universityService.save(university);
        if (save){
            return Result.success("添加成功");
        }
        return Result.fail("添加失败");
    }

    /**
     * 更新大学信息
     * @param university
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody University university){
        log.info("请求参数" + university);
        boolean update = universityService.updateById(university);
        if (update){
            return Result.success("更新成功");
        }
        return Result.fail("更新失败");
    }

    /**
     * 删除大学信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("请求参数" + id);
        boolean remove = universityService.removeById(id);
        if (remove){
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

}
